package com.china08.yxyapi.api.manage.edu.platform.weikecheng;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.yxweike.YxyWeikeService;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

/**
 * 教育局管理员---平台管理---微课管理---下架/审核微课
 * 
 */
@CrossOrigin
@RestController("EduWeikechengAuditController")
public class WeikechengAuditController extends YxyApiBaseController {

    @Autowired
    private YxyWeikeService weikeService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/platform/weikecheng/audit", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {
        reqModel.check();
        YxyProfile yxyProfile = getProfile(request, response);
        weikeService.audit(reqModel.weikeId, reqModel.status, reqModel.remark, yxyProfile);
        // 重建索引
        EsResourceUpdateUtil.updateEsResoure(reqModel.weikeId);
        respModel = new RespModel();
        respModel.setMessage("下架或审核微课成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String weikeId;// 微课id

        public String remark;// 不通过备注

        public Integer status;// 1通过,2不通过,3下架

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(weikeId, null), "微课id不能为空");
            Assert.notNull(status, "审核操作状态值不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }

}
